<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/4/13
 * Time: 16:25
 */

namespace App\Rep;


use App\Models\Material;
use App\Models\WarehouseOrder;
use App\Models\WarehouseOrderChild;
use Illuminate\Support\Facades\DB;

class WarehouseOrderRep
{
    /**
     * 入库单
     * @param $params
     */
    public function createWarehouseOrderRep($params){
        //p($params);
        $type = $params['type']??1;
        DB::beginTransaction();
        try{
            if(isset($params['id']) && !empty($params['id'])){
                $model = WarehouseOrder::where('id',$params['id'])->first();
            }else{
                $model = new WarehouseOrder();
            }

            $model->clearing_form = $params['clearing_form'];//结算方式
            $model->warehouse_id = $params['warehouse_id'];//库房id
            $model->buyer = $params['buyer'];//采购人id
            $model->supply_order_number = $params['supply_order_number'];//供应单号
            $model->warehouse_keeper = $params['warehouse_keeper'];//仓库管理员id
            $model->finance = $params['finance'];//财务id
            $model->prepared_by = $params['prepared_by'];//制单人
            $model->warehouse_entry_time = $params['warehouse_entry_time'];//入库时间
            $model->remark = $params['remark'];//备注
            $model->type = $type;//1:入库单2：出库单
            $model->save();
            //入库单子订单
            $num = 0;
            $money = 0;
            foreach($params['child'] as $k=>$v){
                $insertArr = [];
                $insertArr = [
                    'warehouse_order_id'=>$model->id,//入库单id
                    'serial_number'=>$k+1,//序号
                    'material_id'=>$v['material_id'],//材料id
                    'num'=>$v['num'],//数量
                    'unit_price'=>$v['unit_price'],//单价
                    'money'=>$v['money']??$v['num']*$v['unit_price'],//金额
                    'type'=>$type,
                    'remark'=>$v['remark'],//备注
                    'supplier_id' => $v['supplier_id']//供应商id
                ];
                WarehouseOrderChild::Create($insertArr);
                //材料添加供货型号,修改初期库存
//                $material = Material::find($v['material_id']);
//                $material->model_number = $v['model_number'];
//                if($type == 1){
//                    //入库单
//                    $material->initial_inventory = $material->initial_inventory+$v['num'];
//                    $material->initial_price = $material->initial_price+$v['num']*$v['unit_price'];
//                    //计算数量，金额合计
//                    $num += $v['num'];
//                    $money += $v['money'];
//                }else{
//                    //出库单
//                    $material->initial_inventory = $material->initial_inventory-$v['num'];
//                    $material->initial_price = $material->initial_price-$v['num']*$v['unit_price'];
//                    //计算数量，金额合计
//                    $num -= $v['num'];
//                    $money -= $v['money'];
//                }
//                $material->save();

            }
            $model->num = $num;
            $model->money = $money;
            $model->save();
            DB::commit();
            return ['code'=>0,'data'=>$model,'msg'=>'success'];
        }catch(\Exception $exception){
            DB::rollBack();
            return ['code'=>2001,'data'=>[],'msg'=>$exception->getMessage()];
        }
    }


    /**
     * 获取入库单，出库单记录列表
     * @param $params
     */
    public function getWarehouseOrderListRep($params){
        $list = WarehouseOrder::Search($params)
            ->with(['warehouse'=>function($q){
                $q->select('id','warehouse_name');
            },'buyers'=>function($q){
                $q->select('id','company_name');
            },'warehouse_keepers'=>function($q){
                $q->select('id','company_name');
            },
            'finances'=>function($q){
                $q->select('id','company_name');
            },
            'prepared_bys'=>function($q){
                $q->select('id','company_name');
            }, 'child'=>function($q){
                $q->with(['material','supplier'=>function($q){
                        $q->select('id','name');
                    }]);
                }])
            ->orderBy('id','desc')
            ->paginate($params['per_page']??10);
        return $list;
    }

}
